package com.wb.oldwomen.module;

import com.wb.fw.WorldCamera;
import com.wb.oldwomen.GamePlayLayer;
import com.wb.oldwomen.Ioldwomam;
import com.wiyun.engine.types.WYPoint;

public class ObjControl implements Ioldwomam {
	static ObjControl mObjControl;
	public static ObjControl getInstance(GamePlayLayer parent) {
		if (mObjControl == null) {
			mObjControl = new ObjControl(parent);
		}
		return mObjControl;
	}
	
	BaseConfigData[] mconfigs;
	int mcurConfig;
	GamePlayLayer mparent;
//	Obj mxiepo;
//	Obj moneBox, mtwoBox, mgolds, mapple;
//	private Obj mtarget;
//	private Obj mtree;
//	private Obj mfangzi1;
	Obj[] mobjs;
	protected ObjControl(GamePlayLayer parent) {
		mparent = parent;
		mobjs = new Obj[OBJ_TYPE_COUNT];
		for(int i=0;i<OBJ_TYPE_COUNT;i++) {
			mobjs[i] = new Obj(mparent, i+OBJ_TYPE_HOUSE1);
		}
//		mapple = new Obj(mparent, OBJ_TYPE_APPLE);
//		mfangzi1 = new Obj(mparent, OBJ_TYPE_FANGZI1);
//		mgolds = new Obj(mparent, OBJ_TYPE_GOLD);
//		moneBox = new Obj(mparent, OBJ_TYPE_ONE_BOX);
//		mtarget = new Obj(mparent, OBJ_TYPE_TARGET);
//		mtree = new Obj(mparent, OBJ_TYPE_TREE);
//		mtwoBox = new Obj(mparent, OBJ_TYPE_TWO_BOX);
//		mxiepo = new Obj(mparent, OBJ_TYPE_XIEPO);
	}
	
	
	public void update(float delta) {
		if (mconfigs != null) {
			if (mcurConfig < mconfigs.length) {
				float discamera = WorldCamera.getInstance().getCameraPosx();
				float dismax = discamera + (BASE_VERSION_SCREEN_WIDTH<<1);
				BaseConfigData config = mconfigs[mcurConfig];
				if (config.mdistance < dismax && config.mdistance > discamera + BASE_VERSION_SCREEN_WIDTH) {
					if (config.mname.equals("apple")) {
						setObj(mobjs[OBJ_TYPE_APPLE], config, discamera);
					} else if (config.mname.equals("gold")) {
						setObj(mobjs[OBJ_TYPE_GOLD], config, discamera);
					} else if (config.mname.equals("onebox")) {
						setObj(mobjs[OBJ_TYPE_ONE_BOX], config, discamera);
					} else if (config.mname.equals("twobox")) {
						setObj(mobjs[OBJ_TYPE_TWO_BOX], config, discamera);
					} else if (config.mname.equals("tree")) {
						setObj(mobjs[OBJ_TYPE_TREE], config, discamera);
					} else if (config.mname.equals("house1")) {
						setObj(mobjs[OBJ_TYPE_HOUSE1], config, discamera);
					} else if (config.mname.equals("target")) {
						setObj(mobjs[OBJ_TYPE_TARGET], config, discamera);
					} else if (config.mname.equals("xiepo")) {
						setObj(mobjs[OBJ_TYPE_XIEPO], config, discamera);
					} else {
						
					}
				}
			}
			for(int i=0;i<OBJ_TYPE_COUNT;i++) {
				mobjs[i].update(delta);
			}
		}
	}
	
	void setObj(Obj obj, BaseConfigData config, float discamera) {
		obj.mposx = config.mdistance;
		obj.mposy = config.mheight;
		obj.setVisible(true, WYPoint.make(
				(obj.mposx - discamera),
				(Y_ROAD_RUN + obj.mposy)));
		mcurConfig ++;
	}
	
	public void setLevelData(int level) {
		mcurConfig = 0;
		String[] data = OBJ_LEVLE_CONFIG[level].split(",");
		int cur = 0;
		mconfigs =  new BaseConfigData[data.length/3];
		for (int i=0;i<mconfigs.length;i++) {
			mconfigs[i] = new BaseConfigData();
			mconfigs[i].mname = data[cur++];
			mconfigs[i].mdistance = Integer.valueOf(data[cur++]);
			mconfigs[i].mheight = Integer.valueOf(data[cur++]);
		}
		
	}
	
	public void clearLevelData() {
		for (int i=0;i<mconfigs.length;i++) {
			mconfigs[i] = null;
		}
		mconfigs = null;

		for(int i=0;i<OBJ_TYPE_COUNT;i++) {
//			mobjs[i] = new Obj(mparent, i+OBJ_TYPE_FANGZI1);
			mobjs[i].setVisible(false, null);
		}
	}
	////////////
}
